package com.hellogood.yaohuo.server.mgr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.hellogood.yaohuo.server.DateUtil;
import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.shared.entity.Group;
import com.hellogood.yaohuo.shared.entity.GroupUser;
import com.hellogood.yaohuo.shared.entity.Log;
import com.hellogood.yaohuo.shared.entity.User;
import com.hellogood.yaohuo.shared.vo.GroupVo;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class GroupUserMgr {
	public List<GroupUser> getGroupUserByGroupCode(String groupCode) {
		return DAOFactory.getInstance().createGroupUserDAO().getUserByGroup(groupCode);
	}

	public List<GroupVo> getGroupUser() {
		List<GroupVo> result = new ArrayList<GroupVo>();
		List<GroupUser> list = DAOFactory.getInstance().createGroupUserDAO().getUserGroup();
		List<Group> groupList = DAOFactory.getInstance().createGroupDAO().getAllGroup();

		Map<String, String> groupMap = new HashMap<String, String>();
		for (Group g : groupList) {
			groupMap.put(g.getGroupCode(), g.getGroupName());
		}

		if (list != null) {
			Map<String, String> map = new HashMap<String, String>();
			for (GroupUser u : list) {
				GroupVo vo = new GroupVo();
				vo.setNodeId(u.getGroupCode() + "-" + u.getStaffCode());
				vo.setCode(u.getStaffCode());
				vo.setName(u.getStaffName());
				vo.setParentCode(u.getGroupCode());
				vo.setOpen(false);
				result.add(vo);
				if (!map.containsKey(u.getGroupCode())) {
					map.put(u.getGroupCode(), "");
				}
			}
			Set<String> set = map.keySet();
			Iterator<String> it = set.iterator();
			while (it.hasNext()) {
				GroupVo vo = new GroupVo();
				String code = it.next();
				vo.setNodeId(code);
				vo.setCode(code);
				vo.setName(groupMap.get(code));
				vo.setOpen(false);
				vo.setParentCode("0");
				result.add(vo);
			}
		}
		return result;
	}

	public String addUserToGroup(User u, Group g) {
		String result = DAOFactory.getInstance().createGroupUserDAO().addUserToGroup(u, g);
		if (result.equals("")) {
			int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
			logID++;
			Log log = new Log();
			log.setLogId(logID);
			log.setStaffCode(MainFrame.currentUser.getStaffCode());
			log.setStaffName(MainFrame.currentUser.getStaffName());
			log.setLogDate(DateUtil.currentTime());
			log.setContent("添加人员：" + u.getStaffCode() + "-" + u.getStaffName() + " 到分组：" + g.getGroupCode() + "-" + g.getGroupName());
			DAOFactory.getInstance().createLogDAO().addNewLog(log);
		}
		return result;
	}

	public String addUserToGroup(List<User> users, String groupCode) {
		String result = DAOFactory.getInstance().createGroupUserDAO().addUserToGroup(users, groupCode);
		if (result.equals("")) {
			int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
			logID++;
			Log log = new Log();
			log.setLogId(logID);
			log.setStaffCode(MainFrame.currentUser.getStaffCode());
			log.setStaffName(MainFrame.currentUser.getStaffName());
			log.setLogDate(DateUtil.currentTime());
			log.setContent("添加多个人员到分组：" + groupCode);
			DAOFactory.getInstance().createLogDAO().addNewLog(log);
		}
		return result;
	}

	public String removeUserFromGroup(List<User> users, String groupCode) {
		String result = DAOFactory.getInstance().createGroupUserDAO().removeUserFromGroup(users, groupCode);
		if (result.equals("")) {
			int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
			logID++;
			Log log = new Log();
			log.setLogId(logID);
			log.setStaffCode(MainFrame.currentUser.getStaffCode());
			log.setStaffName(MainFrame.currentUser.getStaffName());
			log.setLogDate(DateUtil.currentTime());
			log.setContent("从分组：" + groupCode + "中删除人员");
			DAOFactory.getInstance().createLogDAO().addNewLog(log);
		}
		return result;
	}

	public String updateUserToGroup(List<User> userList, String groupCode) {
		String result = DAOFactory.getInstance().createGroupUserDAO().removeUserFromGroup(groupCode);
		if (result.equals("")) {
			result = DAOFactory.getInstance().createGroupUserDAO().addUserToGroup(userList, groupCode);
		}
		return result;
	}

	public boolean doesGroupHasOrg(String groupCode) {
		List<GroupUser> list = DAOFactory.getInstance().createGroupUserDAO().getUserByGroup(groupCode);
		if (list != null && list.size() > 0) {
			return true;
		} else {
			return false;
		}

	}
}
